<?php


namespace App\Web\Controller;


use App\Command\ExportFile\ExcelExportHelper;
use App\Facades\UrlCrypt;
use App\Web\Model\CaseModel;
use App\Web\Model\Project as ProjectModel;
use EchoPHP\Http\Request;
use App\Web\Model\CaseResume as CaseResumeModel;

class CaseResume extends Controller {

    /**
     * 获取人才管理简历列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function resumeList(Request $request) {
        $oprId = $this->getLoginId();
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);
        $subId = $request->getParam('subid', 0);
        $projectId = $request->getParam('projectid/d', 0);
        if ($subId <= 0) {
            $resultBody = [
                'status' => false,
                'message' => '请选择客户',
            ];
            return $this->json($resultBody);
        }

        $searchParams = [
            'resumeid' => str_replace('，', ',', $request->getParam('resumeid')),
            'projectid' => $projectId,
            'caseid' => $request->getParam('caseid', ''),
            'flowat' => $request->getParam('flowat', ''),
            'includehis' => $request->getParam('includehis/b', false),
            'casestatus' => $request->getParam('casestatus'),
            'searchtype' => $request->getParam('searchtype'),
            'resumename' => $request->getParam('resumename'),
            'resumephone' => $request->getParam('resumephone'),
            'resumeemail' => $request->getParam('resumeemail'),
            'keycodes' => $request->getParam('keycodes'),
            'starttime' => $request->getParam('starttime'),
            'endtime' => $request->getParam('endtime'),
        ];

        $caseResumeModel = new CaseResumeModel();
        $resultBody = $caseResumeModel->getCaseResumeInfo($oprId, $subId, $projectId, $searchParams, $pageNum, $pageLimit);

        return $this->json($resultBody);
    }

    /**
     * 导出简历索引列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function resumeExcelIndex(Request $request) {
        $oprId = $this->getLoginId();
        $subId = $request->getParam('subid', 0);
        $projectId = $request->getParam('projectid', 0);
        if ($subId <= 0) {
            $resultBody = [
                'status' => false,
                'message' => '请选择客户',
            ];
            return $this->json($resultBody);
        }

        $searchParams = [
            'caseresumeid' => $request->getParam('caseresumeid'),
            'isexport' => true
        ];
        if (empty($searchParams['caseresumeid'])) {
            $resultBody = [
                'status' => false,
                'message' => '请选择导出的简历',
            ];
            return $this->json($resultBody);
        }

        $caseResumeModel = new CaseResumeModel();

        $caseresumelist = array_filter(explode(',', $searchParams['caseresumeid']));

        $caseResumeModel->arrValueToKey($caseresumelist);

        $resumeList = $caseResumeModel->getCaseResumeList($oprId, $subId, $projectId, $searchParams);

        $caseFlowList = config('app.global_caseflow');
        if (!is_file($caseFlowList)) {
            return $this->json([
                'status' => false,
                'message' => '职位流程未定义',
            ]);
        }

        $caseFlow = include $caseFlowList;

        $projectModel = new ProjectModel();
        $projectIds = array_column($resumeList, 'projectid');
        $projectList = $projectModel->getProjectListById($projectIds);

        $caseStatusName = ['01' => '进行中', '02' => '暂停', '03' => '已结束'];
        foreach ($resumeList as $item) {
            $item['flowatinfo'] = '';
            if (!empty($item['flowat']) && !empty($tmpFlow = $caseFlow[$item['flowat']]) && !empty($tmpFlow['name'])) {
                $item['flowatinfo'] = $tmpFlow['name'];
                if (!empty($item['flowatstatus']) && !empty($tmpFlow['child']) && !empty($tmpStatus = $tmpFlow['child'][$item['flowatstatus']]) && !empty($tmpStatus['name'])) {
                    $item['flowatinfo'] .= ' - ' . $tmpStatus['name'];
                }
            }

            $item['casestatuscode'] = $item['casestatus'];
            $item['casestatus'] = isset($caseStatusName[$item['casestatuscode']]) ? $caseStatusName[$item['casestatuscode']] : '';
            $item['projectname'] = isset($projectList[$item['projectid']]) ? $projectList[$item['projectid']]['projectname'] : '';

            $caseresumelist[$item['id']] = $item;
        }

        $exportHelper = new ExcelExportHelper();
        //return $this->json(['status' => true, 'caselist' => $caseresumelist]);
        return $exportHelper->export($caseresumelist, 6, count($caseresumelist));
    }

    /**
     * 获取项目下职位列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getCaseListWithProject(Request $request) {
        $oprId = $this->getLoginId();
        $projectId = $request->getParam('projectid/d');
        $caseStatus = $request->getParam('casestatus', 'all');
        $caseModel = new CaseModel();
        $caseList = $caseModel->getCaseListWithCreator($oprId, $projectId, $caseStatus);
        return $this->json(['status' => true, 'caselist' => $caseList]);
    }
}